'use strict';

const md5 = require('md5');
const ulid = require('../public/ulid');
const { axios } = require('../public/httpHelper');
const configDefault = require('../../config/config.default')();
const qs = require('qs');

const Service = require('egg').Service;

class UserAdminService extends Service {
  async login(params) {
    const result = await this.ctx.model.UserAdmin.findOne({
      where: {
        username: params.username,
        tenantadminId: params.tid,
      },
    });
    return result;
  }
  async list(params) {
    try {
      // const result = await this.ctx.model.UserAdmin.findAndCountAll({
      //   where: {
      //     tenantadminId: params.tid,
      //   },
      //   order: [[ 'createdAt', 'ASC' ]],
      //   offset: parseInt((params.page - 1) * params.limit),
      //   limit: parseInt(params.limit),
      // });
      const config = {
        method: 'get',
        url: `${configDefault.authorization.url}/users?tid=${params.tid}&page=${params.page}&limit=${params.limit}`,
      };
      const result = await axios(config);
      console.log(result);
      return result;
    } catch (error) {
      console.log(error);
    }
  }
  async create(params) {
    const tid = params.tid;
    const password = md5(params.password);
    const result = {
      msg: '',
    };
    const userResult = await this.ctx.model.UserAdmin.findOne({ where: { tenantadminId: tid, username: params.username } });
    if (userResult) {
      result.msg = 'username is used';
      return result;
    }
    try {
      // TODO 这里需要改
      await this.ctx.model.UserAdmin.create({
        id: ulid.ulid(),
        username: params.username,
        password,
        nickname: params.nickname,
        is_super: 0,
        tenantadminId: tid,
        language: params.language,
      });
      result.msg = 'create successful';
      return result;
    } catch (error) {
      result.msg = 'create fail, ' + error;
      return result;
    }
  }
  async update(params) {
    const tid = params.tid;
    const result = {
      msg: '',
    };
    const userResult = await this.ctx.model.UserAdmin.findOne({ where: { tenantadminId: tid, username: params.username } });
    if (userResult) {
      result.msg = 'username is used';
      return result;
    }
    try {
      // TODO 这里需要改
      await this.ctx.model.UserAdmin.update({
        username: params.username,
        nickname: params.nickname,
        language: params.language,
      });
      result.msg = 'create successful';
      return result;
    } catch (error) {
      result.msg = 'create fail, ' + error;
      return result;
    }
  }
  async delete(params) {
    const result = {
      msg: '',
    };
    try {
      await this.ctx.model.UserAdmin.destroy({
        where: { id: params.id },
      });
      result.msg = 'delete successful';
      return result;
    } catch (error) {
      result.msg = 'delete fail, ' + error;
      return result;
    }
  }
  async bindrole(params) {
    const result = {
      msg: '',
    };
    try {
      await this.ctx.modelmysql.UserRole.destroy({ where: { userid: params.id } });
      for (let i = 0; i < params.rolelist.length; i++) {
        await this.ctx.modelmysql.UserRole.create({
          id: ulid.ulid(),
          tid: params.tid,
          userid: params.id,
          roleid: params.rolelist[i],
        });
      }
      result.msg = 'bindrole successful';
      return result;
    } catch (error) {
      result.msg = 'bindrole fail, ' + error;
      return result;
    }
  }
  async getrolelist(params) {
    const result = [];
    const roleResult = await this.ctx.modelmysql.UserRole.findAll({ where: { userid: params.id } });
    for (let i = 0; i < roleResult.length; i++) {
      result.push(roleResult[i].roleid);
    }
    return result;
  }
  async findById(params) {
    const result = await this.ctx.modelmysql.UserRole.findAll(
      {
        where: { userId: params },
        include: [{
          model: this.ctx.modelmysql.RoleInfo,
        }],
      });
    const role = [];
    result.forEach(element => {
      role.push(element.roleid);
    });
    return role;
  }
}


module.exports = UserAdminService;
